package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test3 {
    public static void main(String[] args) throws Exception{
        /*File dir = new File(
                Test2.class.getResource(".").toURI()
        );
        String packName = Test3.class.getPackage().getName();
        System.out.println(packName);
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            Class cls = Class.forName(packName +"."+className);
            Object obj = cls.newInstance();
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getName().contains("s")//是否包含s
                        &&
                    method.getParameterCount()==0//是否无参
                        &&
                    method.getModifiers()== Modifier.PUBLIC//是否公开
                ){
                    method.invoke(obj);//传入
                }
            }
        }*/


        File dir = new File(Test3.class.getResource(".").toURI());
        String Packname = Test3.class.getPackage().getName();
        System.out.println(Packname);
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            Class cls = Class.forName(Packname+"."+className);
            Object obj = cls.newInstance();
            Method [] methods  = cls.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getName().contains("s")
                    &&
                    method.getModifiers() ==Modifier.PUBLIC
                    &&
                    method.getParameterCount()==0
                ){
                    method.invoke(obj);
                }
            }
        }

    }
}
